use std::process::Command;

use serde::Serialize;

#[derive(Serialize)]
pub struct PathNode {
    pub path: String,
    pub path_type: String,
}

pub fn root_path() -> Vec<String> {
    let cmd = "Wmic Logicaldisk Where DriveType=3 Get Name";
    let mut r = do_cmd(cmd);
    r.remove(0);
    return r;
}

pub fn list_path_files(path: &str) -> Vec<PathNode> {
    let cmd = format!("dir {} /b", path);
    let r = do_cmd(cmd.as_str());
    let rr = r
        .into_iter()
        .map(|e| PathNode {
            path: e,
            path_type: "file".to_string(),
        })
        .collect();
    return rr;
}

fn do_cmd(cmd: &str) -> Vec<String> {
    let output: std::process::Output = Command::new("cmd")
        .arg("/c")
        .arg(cmd)
        .output()
        .expect(format!("cmd exec error : {}", cmd).as_str());
    // 输出命令的标准输出
    let stdout = String::from_utf8_lossy(&output.stdout);
    println!("Standard Output:\n{}", stdout);
    let r: Vec<String> = stdout
        .split("\r\n")
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .map(|f: &str| f.to_string())
        .collect();
    return r;
}
